#include "window_common.h"
#include "gkt_voice.h"

#if !GKT_VOICE_OUTPUT_VOLUME_DISABLE

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_LIST,
	WIDGET_ID_MAX
};

static const gkt_widget_list_item_s sc_window_voice_volume_widget_list_items[] = {
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_volume_high, GKT_VOICE_OUTPUT_VOLUME_HIGH),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_volume_middle, GKT_VOICE_OUTPUT_VOLUME_MIDDLE),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_TEXT_ID_volume_low, GKT_VOICE_OUTPUT_VOLUME_LOW),
};
#define WINDOW_VOICE_VOLUME_WIDGET_LIST_ITEMS_SIZE	\
	(sizeof(sc_window_voice_volume_widget_list_items) / sizeof(gkt_widget_list_item_s))

static const gkt_widget_list_attr_s sc_window_voice_volume_widget_list_attr = 
	GKT_WIDGET_LIST_STATIC_ATTR(WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_VOICE_VOLUME_WIDGET_LIST_ITEMS_SIZE,
		sc_window_voice_volume_widget_list_items);

GKT_WINDOW_WIDGET_DESCS_DEFINE(voice_volume) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_voice_volume_adjustment
	),
	[WIDGET_ID_LIST] = GKT_WIDGET_LIST_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT*(WINDOW_NORMAL_ROWS - 1),
		&sc_window_voice_volume_widget_list_attr
	),
};

typedef struct {
	gkt_voice_output_volume_e	voice_volume;
} window_voice_volume_s;
static window_voice_volume_s s_window_voice_volume;

static void window_voice_volume_complete(void)
{
	if ((uint32_t)s_window_voice_volume.voice_volume != gp_window_data->voice_volume) {
		gp_window_data->voice_volume = s_window_voice_volume.voice_volume;
		window_data_save();
	}

	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_SUCCESS,
			WINDOW_TEXT_ID_voice_volume_adjustment,
			WINDOW_TEXT_ID_set_success, WINDOW_VOICE_COMBINED_ID(set_success));

	gkt_window_exit();
}

static int window_voice_volume_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	gkt_voice_output_volume_e voice_volume;
	uint32_t item_index;
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_INIT == minor_type) {
				if (gp_window_data->voice_volume >= GKT_VOICE_OUTPUT_VOLUME_MAX)
					gp_window_data->voice_volume = GKT_VOICE_OUTPUT_VOLUME_DEFAULT;
				s_window_voice_volume.voice_volume = gp_window_data->voice_volume;
			}
			else if (GKT_WINDOW_ACTIVE == minor_type) {
				item_index = 0;
				while (item_index < WINDOW_VOICE_VOLUME_WIDGET_LIST_ITEMS_SIZE) {
					voice_volume = sc_window_voice_volume_widget_list_items[item_index].data;
					if (voice_volume == s_window_voice_volume.voice_volume) {
						gkt_widget_list_set_focus(WIDGET_ID_LIST, item_index);
						break;
					}
					item_index++;
				}
				gkt_widget_enable_item_focus_by_id(WIDGET_ID_LIST);

				gkt_key_voice_mask(~0);
				window_common_mask_except_updn_cancel_ok();
			}
			else if (GKT_WINDOW_UNINIT == minor_type)
				gkt_voice_output_set_volume(gp_window_data->voice_volume);
			break;
		case GKT_EVENT_WIDGET:
			GKT_ASSERT(data < WINDOW_VOICE_VOLUME_WIDGET_LIST_ITEMS_SIZE);

			if (GKT_WIDGET_EVENT_ITEM_FOCUS == minor_type) {
				s_window_voice_volume.voice_volume = 
					sc_window_voice_volume_widget_list_items[data].data;
				gkt_voice_output_set_volume(s_window_voice_volume.voice_volume);
				gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(key), 
						GKT_WINDOW_VOICE_F_DETACHED, NULL);
			}
			else if (GKT_WIDGET_EVENT_CLICK == minor_type)
				window_voice_volume_complete();

			retval = GKT_EVENT_HANDLE_COMPLETE;
			break;
		case GKT_EVENT_KEY:
			if (GKT_KEY_BACKSPACE == minor_type) {
				gkt_voice_output_set_volume(gp_window_data->voice_volume);
				gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(key), 
						GKT_WINDOW_VOICE_F_DETACHED, NULL);
			}
			break;
		default:
			break;
	}
	
	return retval;
}
GKT_WINDOW_DESC_DEFINE(voice_volume);

#endif

